package com.huangguolin.cloud.api.role.service.impl;

import com.huangguolin.cloud.api.role.entity.ApiRoleSearchEntity;
import com.huangguolin.cloud.api.role.repository.ApiRoleDao;
import com.huangguolin.cloud.api.role.service.ApiRoleService;
import com.huangguolin.cloud.mybatis.entity.PtRoleEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.mybatis.ztree.ChildTree;
import com.huangguolin.cloud.mybatis.ztree.ZtreeEntity;
import com.huangguolin.cloud.role.repository.RoleDao;
import com.huangguolin.cloud.role.service.RoleService;
import com.huangguolin.cloud.spring.util.CloudUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApiRoleServiceImpl implements ApiRoleService {
    @Resource
    private ApiRoleDao apiRoleDao;
    @Resource
    private RoleService roleService;

    @Resource
    private RoleDao roleDao;

    @Override
    public List<PtRoleEntity> queryRoleList(ApiRoleSearchEntity entity, PagingEntity paging) {
        return apiRoleDao.queryRoleList(entity,paging);
    }

    @Override
    public Integer queryRoleTotal(ApiRoleSearchEntity entity) {
        return apiRoleDao.queryRoleTotal(entity);
    }


    @Override
    public List<ChildTree> queryMenuTree() {
        List<ZtreeEntity> list = roleService.queryMenuZtreeAll();

        List<ChildTree> childTrees= CloudUtil.changeChildTree(list);
        return childTrees;
    }

    @Override
    public void saveMenRefRole(String roleUuids, String menuCheckIds, String menuHalfIds) {
        String[] roleArray = roleUuids.split(",");
        String[] menuCheckArray = menuCheckIds.split(",");
        String[] menuHalfArray = menuHalfIds.split(",");
        //遍历roleId
        for(String roleId : roleArray){
            //删除
            roleDao.deleteMenuByRole(roleId);
            for (String checkMenuId : menuCheckArray){
                //插入
                roleDao.insertMenuByRole(roleId,checkMenuId);
            }
            for(String halfMenuId :menuHalfArray){
                apiRoleDao.insertMenuByRole(roleId,halfMenuId);
            }
        }
    }

    @Override
    public List<String> queryAllowMenuRst(String roleUuid) {
        return apiRoleDao.queryAllowMenuRst(roleUuid);
    }

}
